//
//  SearchHeadCell.m
//  ProjectsOne
//
//  Created by iOS开发 on 2018/7/11.
//  Copyright © 2018年 iOS开发. All rights reserved.
//

#import "SearchHeadCell.h"

@interface SearchHeadCell ()
@property (nonatomic, strong) UIImageView *styleImageView;
@property (nonatomic, strong) UILabel *titleLabel;
@property (nonatomic, strong) UIButton *deleteButton;
@end

@implementation SearchHeadCell

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        [self createView];
    }
    return self;
}

- (void)createView {
    _styleImageView = [[UIImageView alloc] init];
    [self addSubview:_styleImageView];
    
    [_styleImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).with.offset(25);
        make.left.mas_equalTo(self).with.offset(15);
    }];
    
    _titleLabel = [[UILabel alloc] init];
    _titleLabel.font = [UIFont systemFontOfSize:16];
    _titleLabel.textColor = [UIColor colorWithHexString:@"999999"];
    [self addSubview:_titleLabel];
    
    [_titleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.mas_equalTo(self.styleImageView);
        make.left.mas_equalTo(self.styleImageView.mas_right).with.offset(8);
    }];
    
    _deleteButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _deleteButton.hidden = YES;
    [_deleteButton setImage:[UIImage imageNamed:@"search_delete"] forState:UIControlStateNormal];
    [_deleteButton addTarget:self action:@selector(delete:) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_deleteButton];
    
    [_deleteButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(self).with.offset(-20);
        make.centerY.mas_equalTo(self.styleImageView);
        make.width.height.mas_equalTo(40);
    }];
}

- (void)setStyle:(NSString *)style {
    UIImage *image;
    if ([style isEqualToString:@"历史搜索"]) {
        _deleteButton.hidden = NO;
        image = [UIImage imageNamed:@"search_history"];
    } else {
        _deleteButton.hidden = YES;
        image = [UIImage imageNamed:@"search_hot"];
    }
    _styleImageView.image = image;
    _titleLabel.text = style;
}

- (void)delete:(UIButton *)button {
    if (self.dbBlock) {
        self.dbBlock();
    }
}

@end
